<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修复验证测试</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/ilovefree-migration.css">
    <link rel="stylesheet" href="assets/css/enhanced-layout.css">
    <link rel="stylesheet" href="assets/css/fontawesome-icons.css">
    <style>
        .test-container {
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
        .debug-log {
            background: #2d3748;
            color: #e2e8f0;
            padding: 15px;
            border-radius: 8px;
            font-family: monospace;
            font-size: 14px;
            margin: 20px 0;
            max-height: 300px;
            overflow-y: auto;
        }
        .success { color: #48bb78; }
        .error { color: #f56565; }
        .info { color: #4299e1; }
    </style>
</head>
<body>
    <div class="test-container">
        <h1>🔧 标签过滤修复验证</h1>

        <?php
        require_once 'config/config.php';
        require_once 'includes/database.php';
        $db = new Database();

        // 获取分类2的数据
        $category = null;
        $categories = $db->getCategories();
        foreach ($categories as $cat) {
            if ($cat['id'] == 2) {
                $category = $cat;
                break;
            }
        }

        $navigations = $db->getNavigations(2);
        $tags = $db->getTags(2);

        // 获取推荐标签信息
        $recommendTagId = 0;
        foreach ($tags as $tag) {
            if ($tag['name_cn'] === '推荐') {
                $recommendTagId = $tag['id'];
                break;
            }
        }

        // 计算推荐标签数量
        $recommendCount = 0;
        if ($recommendTagId > 0) {
            foreach ($navigations as $nav) {
                if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                    if (in_array($recommendTagId, $nav['tag_ids'])) {
                        $recommendCount++;
                    }
                }
            }
        }
        ?>

        <div class="debug-log" id="debug-log">
            <div class="info">🔍 系统调试日志
        

        <!-- 标签容器 -->
        <div class="category-header">
            <div class="category-title">
                <div class="category-icon">
                <h2 class="category-name"><?php echo htmlspecialchars($category['name']); ?></h2>
            
            <div class="tags-container" id="category-<?php echo $category['id']; ?>">
                <!-- 推荐标签 -->
                <button class="tag-btn active"
                        data-tag-id="<?php echo $recommendTagId; ?>"
                        data-tag-filter="推荐"
                        data-category-id="<?php echo $category['id']; ?>">
                    推荐
                    <span class="tag-count"><?php echo $recommendCount; ?></span>
                </button>

                <?php
                // 过滤掉推荐标签
                $filteredTags = array_filter($tags, function($tag) {
                    return trim($tag['name_cn']) !== '推荐';
                });
                ?>
                <?php foreach ($filteredTags as $tag): ?>
                    <?php
                    // 计算该标签下的导航数量
                    $tagNavCount = 0;
                    foreach ($navigations as $nav) {
                        if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                            if (in_array($tag['id'], $nav['tag_ids'])) {
                                $tagNavCount++;
                            }
                        }
                    }
                    ?>
                    <button class="tag-btn"
                            data-tag-id="<?php echo $tag['id']; ?>"
                            data-tag-filter="<?php echo htmlspecialchars($tag['name_cn']); ?>"
                            data-category-id="<?php echo $category['id']; ?>">
                        <?php echo htmlspecialchars($tag['name_cn']); ?>
                        <span class="tag-count"><?php echo $tagNavCount; ?></span>
                    </button>
                <?php endforeach; ?>
            
        

        <!-- 导航卡片 -->
        <div class="nav-grid" id="nav-grid-<?php echo $category['id']; ?>">
            <?php foreach ($navigations as $nav): ?>
                <?php
                // 构建标签过滤器
                $tagFilters = [];
                if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                    foreach ($nav['tag_ids'] as $tagId) {
                        foreach ($tags as $tag) {
                            if ($tag['id'] == $tagId) {
                                $tagFilters[] = htmlspecialchars($tag['name_cn']);
                                break;
                            }
                        }
                    }
                }
                // 添加"推荐"标签到所有导航卡片
                $tagFilters[] = '推荐';
                $tagFiltersString = implode(',', array_unique($tagFilters));
                ?>
                <div class="nav-card"
                     data-tag-filters="<?php echo $tagFiltersString; ?>">
                    <a href="<?php echo htmlspecialchars($nav['url']); ?>" target="_blank">
                        
                            <h4><?php echo htmlspecialchars($nav['name']); ?></h4>
                            <p><?php echo htmlspecialchars($nav['description']); ?></p>
                        
                    </a>
                
            <?php endforeach; ?>
        
    

    <script src="assets/js/php-ilovefree-migration.js"></script>
    <script>
        function log(message, type = 'info') {
            const timestamp = new Date().toLocaleTimeString();
            const logDiv = document.getElementById('debug-log');
            const className = type === 'error' ? 'error' : (type === 'success' ? 'success' : 'info');
            logDiv.innerHTML += `<div class="${className}">[${timestamp}] ${message}`;
            logDiv.scrollTop = logDiv.scrollHeight;
            console.log(message);
        }

        document.addEventListener('DOMContentLoaded', () => {
            log('🎯 页面加载完成', 'success');
            log(`📊 导航卡片总数: ${document.querySelectorAll('.nav-card').length}`);
            log(`📊 标签按钮总数: ${document.querySelectorAll('.tag-btn').length}`);

            // 等待迁移系统初始化
            setTimeout(() => {
                if (window.phpPickfreeMigration) {
                    log('✅ PHP迁移系统已初始化', 'success');

                    // 检查分类数据
                    const categoryId = '<?php echo $category['id']; ?>';
                    const categoryData = window.phpPickfreeMigration.categories[categoryId];
                    if (categoryData) {
                        log(`📊 分类${categoryId}数据: ${categoryData.cards.length}个卡片`);
                        log(`📊 分类${categoryId}标签统计: ${JSON.stringify(categoryData.tagCounts)}`);

                        // 测试推荐标签
                        const recommendCount = categoryData.cards.filter(card =>
                            card.tags.includes('推荐')
                        ).length;
                        log(`🎯 推荐标签应该显示: ${recommendCount}个卡片`, 'success');

                        // 验证当前显示的卡片数量
                        const visibleCards = categoryData.cards.filter(card =>
                            card.element.style.display !== 'none' &&
                            !card.element.classList.contains('hidden')
                        ).length;
                        log(`👁️ 当前显示卡片数: ${visibleCards}`);

                        if (visibleCards === recommendCount) {
                            log('✅ 推荐标签显示正常', 'success');
                        } else {
                            log(`❌ 推荐标签显示异常: 期望${recommendCount}个，实际${visibleCards}个`, 'error');
                        }
                    } else {
                        log(`❌ 分类${categoryId}数据未找到`, 'error');
                    }
                } else {
                    log('❌ PHP迁移系统未初始化', 'error');
                }
            }, 2000);
        });

        // 监听标签点击
        document.addEventListener('click', (e) => {
            if (e.target.classList.contains('tag-btn')) {
                const tagId = e.target.getAttribute('data-tag-id');
                const tagFilter = e.target.getAttribute('data-tag-filter');
                log(`🏷️ 点击标签: ID=${tagId}, Filter="${tagFilter}"`);

                // 延迟检查结果
                setTimeout(() => {
                    const categoryId = '<?php echo $category['id']; ?>';
                    const categoryData = window.phpPickfreeMigration?.categories[categoryId];
                    if (categoryData) {
                        const visibleCards = categoryData.cards.filter(card =>
                            card.element.style.display !== 'none' &&
                            !card.element.classList.contains('hidden')
                        ).length;
                        log(`👁️ 点击后显示卡片数: ${visibleCards}`);

                        if (visibleCards > 0) {
                            log('✅ 标签过滤工作正常', 'success');
                        } else {
                            log('❌ 标签过滤无结果显示', 'error');
                        }
                    }
                }, 500);
            }
        });
    </script>
</body>
</html>